<?php
declare (strict_types = 1);

namespace app;

use think\App;
use think\exception\ValidateException;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {}

    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }



    /**
     * 接口返回参数
     * @param int $code 200 成功 400 失败
     * @param string $msg
     * @param array $data
     */
    protected function ajaxReturn($code = 200, $msg = "success", $data = array())
    {
        header('Content-Type:text/json;charset=utf-8');
        if($code==200){
            $result = array(
                "code" => $code,
                "msg"  => $msg,
                "data" => $data==null?array():$data,
            );
            p($result);
            exit(json_encode($result));
        }
        if($code==400){
            $result = array(
                "code" => $code,
                "msg"  => $msg,
                "data" => $data==null?array():$data,
            );
            p($result);
            exit(json_encode($result));
        }
    }


    // 
    function md5Encryption($post_data)
    {
        foreach ($post_data as $k => &$v) {
            if(is_array($v)){
                unset($post_data[$k]);
            }
            
        };

        $post_data['time_stamp'] = time();
        ksort($post_data);
        $post_data['sign'] = md5( implode('#', $post_data) . 'orderYue' );
        //print_r($post_data);
        return $post_data;
     
    }



    // 验证 md5加密数据sign
    function checkMd5Encryption($post_data)
    {
        foreach ($post_data as $k => &$v) {
            if(is_array($v)){
                unset($post_data[$k]);
            }
        };
        if(!isset($post_data['time_stamp'])):
            exit(json_encode(['code'=>400,'msg'=>'time_stamp  no null!', 'data'=>'']));
        endif;

        if(!isset($post_data['sign'])):
            exit(json_encode(['code'=>400,'msg'=>'sign no null!', 'data'=>'']));
        endif;


        // 验证有效期【60秒】
        if( ($post_data['time_stamp']+60) < time() ):
     
            exit(json_encode(['code'=>400,'msg'=>'overtime!', 'data'=>'']));
            
        endif;
        // 验证签名
        $sign = $post_data['sign'];
        unset($post_data['sign']);
        ksort($post_data);
        if($sign != md5( implode('#', $post_data) . 'orderYue' ) ):
            exit(json_encode(['code'=>400,'msg'=>'sign error!', 'data'=>'']));
      
        endif;
    }


}
